import React, { useEffect, useRef, useState } from 'react'

function useThrottle(fun, time) {
    // 定义计时器
    let jsq = useRef()
    useEffect(() => {
        return () => {
            if (jsq.current) {
                clearTimeout(jsq.current)
            }
        }
    }, [])
    return (...val) => {
        // 判读那计时器是否开启
        if (jsq.current) {
            clearTimeout(jsq.current)
        }
        jsq.current = setTimeout(() => {
            fun(...val)
        }, time)

    }

}

export default useThrottle
